
* REPLICATION CODE: JENNINGS & WLEZIEN, 'THE COMPARATIVE TIMELINE OF ELECTIONS', AMERICAN JOURNAL OF POLITICAL SCIENCE *

* FIGURE S6. Root Mean Squared Errors (US, UK and Germany)

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS *********************
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=.
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************
keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_RMSE_EQ1.dta, replace

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS + MP + ECON *********
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ mp_ ics_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=.
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_RMSE_EQ5_X1.dta, replace

****** SUPPLEMENTARY FIGURE S6 **************************************************************
*Root Mean Squared Errors (US, UK and Germany)
use MI_RMSE_EQ1.dta, clear 
rename rmse eq1_rmse
rename rmse_min95 eq1_rmse_min95
rename rmse_max95 eq1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_RMSE_EQ1.dta, replace

use MI_RMSE_EQ5.dta, clear 
rename rmse eq5_rmse
rename rmse_min95 eq5_rmse_min95
rename rmse_max95 eq5_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_RMSE_EQ5.dta, replace

merge daysbeforeED using MI_RMSE_EQ1.dta

* 
twoway /*
*/  || line eq1_rmse daysbeforeED,                       /*
*/  clpattern(solid) clcolor(black) clwidth(medthick)    /* 
*/  || line eq5_rmse daysbeforeED,                       /*
*/  clpattern(dash) clcolor(black) clwidth(medthick)     /* 
*/  || line eq1_rmse_min95 daysbeforeED,                 /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line eq1_rmse_max95 daysbeforeED,                 /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line eq5_rmse_min95 daysbeforeED,                 /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line eq5_rmse_max95 daysbeforeED,                 /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  , scheme(s2mono) graphregion(color(white))           /*
*/  xtitle("Days Until Election")                        /*
*/  title("")                                            /*
*/  ylabel(0(1)8, gmax angle(horizontal))                /*
*/  ytick(0(1)8)                                         /*
*/  ytitle(Root Mean Squared Error)                      /*
*/  xlabel(0(50)200)                                     /*
*/  xscale(reverse) xscale(titlegap(2))                  /*
*/  legend(order(1 "Vote = Polls" 2 "Vote = Polls + Macro-Partisanship + Economic Perceptions") /*
*/  rows(2)                                              /*
*/  size(medsmall))

graph export FigS6.tif
